﻿using ConsoleApplication.WebAPI;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace NS
{
    [HotUpdate]
    [Description("供应商新增表审核后新增供应商")]
    public class CreateSupplierService : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.PreparePropertysEventArgs e)
        {
            e.FieldKeys.Add("FID");
            e.FieldKeys.Add("F_WNEG_Assistant1");
            e.FieldKeys.Add("FNumber");
            e.FieldKeys.Add("FName");
            e.FieldKeys.Add("F_WNEG_Group1");
            e.FieldKeys.Add("F_WNEG_Base3");
            e.FieldKeys.Add("FSUPID");
            e.FieldKeys.Add("F_WNEG_Text2");
            e.FieldKeys.Add("FCountry");
            e.FieldKeys.Add("FLegalPerson");
            e.FieldKeys.Add("FSOCIALCRECODE");
            e.FieldKeys.Add("F_WNEG_Text4");
            e.FieldKeys.Add("F_WNEG_Remark");
            e.FieldKeys.Add("F_WNEG_Remark12");
            e.FieldKeys.Add("F_WNEG_Date");
            e.FieldKeys.Add("F_WNEG_Remark13");
            e.FieldKeys.Add("FALLNAME");
            e.FieldKeys.Add("F_WNEG_MulBase");
            e.FieldKeys.Add("F_WNEG_STAFFID");
            e.FieldKeys.Add("FCreatorId");
            e.FieldKeys.Add("F_WNEG_Base");
            e.FieldKeys.Add("F_WNEG_Base1");
            base.OnPreparePropertys(e);
        }

        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);
        
        string F_WNEG_Assistant1 = "", MEssage = "", FNumber = "", FSUPID = "", FName = "", FSupplierId = "", F_WNEG_Group1 = "", F_WNEG_Base3 = "", F_WNEG_Text2 = "", FALLNAME="",
                    FCountry = "", FLegalPerson = "", FSOCIALCRECODE = "", F_WNEG_Text4 = "", F_WNEG_Remark = "", F_WNEG_Remark12 = "", F_WNEG_Date = "", F_WNEG_Remark13 = "",FID="",
                    FdataValue = "", SUMFdataValue = "", F_WNEG_STAFFID="", FCreatorld="",F_WNEG_Base="", F_WNEG_Base1="";
            foreach (DynamicObject item in e.DataEntitys)
            {
                if (item["ID"] != null && Convert.ToString(item["ID"]) != "")
                {
                    FID = Convert.ToString(item["ID"]);
                }
                if (item["F_WNEG_Assistant1"] != null && Convert.ToString(item["F_WNEG_Assistant1"]) != "")
                {
                    F_WNEG_Assistant1 = Convert.ToString((item["F_WNEG_Assistant1"] as DynamicObject)["FNumber"]);
                }
                if (item["Number"] != null && Convert.ToString(item["Number"]) != "")
                {
                    FNumber = Convert.ToString(item["Number"]);
                }
                if (item["Name"] != null && Convert.ToString(item["Name"]) != "")
                {
                    FName = Convert.ToString(item["Name"]);
                }
                if (item["F_WNEG_Group1"] != null && Convert.ToString(item["F_WNEG_Group1"]) != "")
                {
                    F_WNEG_Group1 = Convert.ToString((item["F_WNEG_Group1"] as DynamicObject)["Number"]);
                }
                if (item["F_WNEG_Base3"] != null && Convert.ToString(item["F_WNEG_Base3"]) != "")
                {
                    F_WNEG_Base3 = Convert.ToString((item["F_WNEG_Base3"] as DynamicObject)["Name"]);
                }
                if (item["FSUPID"] != null && Convert.ToString(item["FSUPID"]) != "")
                {
                    FSUPID = Convert.ToString(item["FSUPID"]);
                }
                if (item["F_WNEG_Text2"] != null && Convert.ToString(item["F_WNEG_Text2"]) != "")
                {
                    F_WNEG_Text2 = Convert.ToString(item["F_WNEG_Text2"]);
                }
                if (item["Country"] != null && Convert.ToString(item["Country"]) != "")
                {
                    FCountry = Convert.ToString((item["Country"] as DynamicObject)["FNumber"]);
                }
                if (item["LegalPerson"] != null && Convert.ToString(item["LegalPerson"]) != "")
                {
                    FLegalPerson = Convert.ToString(item["LegalPerson"]);
                }
                if (item["SOCIALCRECODE"] != null && Convert.ToString(item["SOCIALCRECODE"]) != "")
                {
                    FSOCIALCRECODE = Convert.ToString(item["SOCIALCRECODE"]);
                }
                if (item["F_WNEG_Text4"] != null && Convert.ToString(item["F_WNEG_Text4"]) != "")
                {
                    F_WNEG_Text4 = Convert.ToString(item["F_WNEG_Text4"]);
                }
                if (item["F_WNEG_Remark"] != null && Convert.ToString(item["F_WNEG_Remark"]) != "")
                {
                    F_WNEG_Remark = Convert.ToString(item["F_WNEG_Remark"]);
                }
                if (item["F_WNEG_Remark12"] != null && Convert.ToString(item["F_WNEG_Remark12"]) != "")
                {
                    F_WNEG_Remark12 = Convert.ToString(item["F_WNEG_Remark12"]);
                }
                if (item["F_WNEG_Date"] != null && Convert.ToString(item["F_WNEG_Date"]) != "")
                {
                    F_WNEG_Date = Convert.ToString(item["F_WNEG_Date"]);
                }
                if (item["F_WNEG_Remark13"] != null && Convert.ToString(item["F_WNEG_Remark13"]) != "")
                {
                    F_WNEG_Remark13 = Convert.ToString(item["F_WNEG_Remark13"]);
                }
                if (item["FALLNAME"] != null && Convert.ToString(item["FALLNAME"]) != "")
                {
                    FALLNAME = Convert.ToString(item["FALLNAME"]);
                }
                if (item["F_WNEG_MulBase"] != null && Convert.ToString(item["F_WNEG_MulBase"]) != "")
                {
                    DynamicObjectCollection F_WNEG_MulBase = item["F_WNEG_MulBase"] as DynamicObjectCollection;
                    if (F_WNEG_MulBase.Count > 0)
                    {
                        foreach (var item12 in F_WNEG_MulBase)
                        {
                            FdataValue = Convert.ToString((item12["F_WNEG_MulBase"] as DynamicObject)["Name"]);
                            SUMFdataValue = SUMFdataValue + "," + FdataValue;
                        }
                        SUMFdataValue = SUMFdataValue.TrimEnd(',');
                        SUMFdataValue = SUMFdataValue.TrimStart(',');
                    }
                }
                if (item["F_WNEG_STAFFID"] != null && Convert.ToString(item["F_WNEG_STAFFID"]) != "")
                {
                    F_WNEG_STAFFID = Convert.ToString((item["F_WNEG_STAFFID"] as DynamicObject)["Number"]);
                }
                if (item["CreatorId"] != null && Convert.ToString(item["CreatorId"]) != "")
                {
                    FCreatorld = Convert.ToString((item["CreatorId"] as DynamicObject)["Name"]);
                }
                if (item["F_WNEG_Base"] != null && Convert.ToString(item["F_WNEG_Base"]) != "")
                {
                    F_WNEG_Base = Convert.ToString((item["F_WNEG_Base"] as DynamicObject)["Number"]);
                }
                if (item["F_WNEG_Base1"] != null && Convert.ToString(item["F_WNEG_Base1"]) != "")
                {
                    F_WNEG_Base1 = Convert.ToString((item["F_WNEG_Base1"] as DynamicObject)["Number"]);
                }
                if (!FSUPID.Equals(""))
                {
                    string SQL0 = "/*dialect*/" + string.Format(@"select FSupplierId from t_BD_Supplier where fnumber='{0}'
                    ", F_WNEG_Assistant1 + FNumber);
                    DynamicObjectCollection DynamData0 = DBUtils.ExecuteDynamicObject(this.Context, SQL0);
                    if (DynamData0.Count > 0)
                    {
                        FSupplierId = Convert.ToString(DynamData0[0]["FSupplierId"]);
                        MEssage = "修改供应商成功！";
                    }
                }
                else
                {
                    FSupplierId = "0";
                    MEssage = "新增供应商成功！";
                }
                var result = InvokeHelper.Login();
                var iResult = JObject.Parse(result)["LoginResultType"].Value<int>();
                if (iResult == 1)
                {
                    JObject json = new JObject();
                    json.Add("IsDeleteEntry", "true");
                    json.Add("ValidateFlag", "true");
                    json.Add("IsAutoAdjustField", "true");
                    json.Add("IsAutoSubmitAndAudit", "true");
                    JObject item1 = new JObject();
                    item1.Add("FSupplierId", FSupplierId);
                    JObject Obj = new JObject();
                    Obj.Add("FNumber", "100");
                    item1.Add("FCreateOrgId", Obj);
                    Obj = new JObject();
                    Obj.Add("FNumber", "100");
                    item1.Add("FUseOrgId", Obj);
                    Obj = new JObject();
                    Obj.Add("FNumber", F_WNEG_Group1);
                    item1.Add("FGroup", Obj);
                    item1.Add("FNumber", F_WNEG_Assistant1 + FNumber);
                    item1.Add("FName", FName);
                    item1.Add("F_WNEG_Text3", F_WNEG_Text2);
                    //item1.Add("F_WNEG_Text4", F_WNEG_Text4);
                    item1.Add("F_WNEG_Remark", F_WNEG_Remark);
                    item1.Add("F_WNEG_Remark12", F_WNEG_Remark12);
                    item1.Add("F_WNEG_Date", F_WNEG_Date);
                    item1.Add("F_WNEG_Remark13", F_WNEG_Remark13);
                    item1.Add("FShortName", FALLNAME);
                    item1.Add("F_WNEG_Text4", SUMFdataValue);
                    item1.Add("F_WNEG_Text14", FCreatorld);
                    Obj = new JObject();
                    Obj.Add("FNumber", F_WNEG_Base);
                    item1.Add("F_WNEG_Base1", Obj);
                    Obj = new JObject();
                    Obj.Add("FNumber", F_WNEG_Base1);
                    item1.Add("F_WNEG_Base2", Obj);

                    JObject FFinanceInfo = new JObject();
                    Obj = new JObject();
                    Obj.Add("FName", F_WNEG_Base3);
                    FFinanceInfo.Add("FPayCondition", Obj);
                    item1.Add("FFinanceInfo", FFinanceInfo);

                    JObject FBaseInfo = new JObject();

                    Obj = new JObject();
                    Obj.Add("FNumber", FCountry);
                    FBaseInfo.Add("FCountry", Obj);
                    Obj = new JObject();
                    Obj.Add("FNumber", F_WNEG_STAFFID);
                    FBaseInfo.Add("FSTAFFID", Obj);
                    FBaseInfo.Add("FSOCIALCRECODE", FSOCIALCRECODE);
                    FBaseInfo.Add("FLEGALPERSON", FLegalPerson);
                    item1.Add("FBaseInfo", FBaseInfo);

                    json.Add("Model", item1);
                    result = InvokeHelper.Save("BD_Supplier", json.ToString());
                    var result0 = JObject.Parse(result);
                    var IsSuccess = result0["Result"]["ResponseStatus"]["IsSuccess"];
                    if (IsSuccess.ToString().Equals("True"))
                    {
                        var SuccessEntitysId = result0["Result"]["ResponseStatus"]["SuccessEntitys"][0]["Id"];
                        //this.Model.SetValue("FSUPID", SuccessEntitysId.ToString());
                        string SQL1 = "/*dialect*/" + string.Format(@"update WLLZ_t_Cust100020 set FSUPID='{0}',F_WNEG_Remark1='{2}' where fid='{1}'
                        ", SuccessEntitysId.ToString(),FID, result);
                        DBUtils.ExecuteDynamicObject(this.Context, SQL1);
                        //this.View.UpdateView("FSUPID");
                        //this.View.ShowMessage(MEssage);
                    }
                    else
                    {
                        var Errors = result0["Result"]["ResponseStatus"]["Errors"];
                        string SQL1 = "/*dialect*/" + string.Format(@"update WLLZ_t_Cust100020 set F_WNEG_Remark1='{0}' where fid='{1}'
                        ", Errors,FID);
                        DBUtils.ExecuteDynamicObject(this.Context, SQL1);
                        //this.View.ShowErrMessage("新增或修改供应商失败！+'" + Errors + "'");
                        throw new Exception(Errors.ToString());
                    }
                }
            }
        }
    }
}
